import interfaces from "./interfaces";
import { METADATA_KEY } from "./constants";
import "reflect-metadata";

export default function Provide(serviceIdentifier: string, args?: Array<any>) {
  return function (target: any) {
    // 当前 class 的信息
    const currentMetadata: interfaces.ProvideSyntax = {
      id: serviceIdentifier,
      args: args || [],
      clazz: target
    };
    // 已经收集的 class
    const previousMetadata: interfaces.ProvideSyntax[] = Reflect.getMetadata(
      METADATA_KEY.provide,
      Reflect
    ) || [];

    const newMetadata = [currentMetadata, ...previousMetadata];

    // 将所有使用 Provide 装饰器标识的 class 都作为 Reflect 的元素据暂存
    Reflect.defineMetadata(
      METADATA_KEY.provide,
      newMetadata,
      Reflect
    );
    return target;
  };
}
